package com.iogogogo.supervisord4j.api;

import com.iogogogo.supervisord.base.BaseResult;
import com.iogogogo.supervisord.base.ResponseWrapper;
import com.iogogogo.supervisord.properties.SupervisordProperties;
import com.iogogogo.supervisord4j.base.context.SpringContextHolder;
import com.iogogogo.supervisord4j.pojo.request.InstanceBeanRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created by tao.zeng on 2022/1/27.
 * <p>
 * https://www.cnblogs.com/eternityz/p/12241143.html
 */
@Api(value = "supervisord4j", tags = "Bean Api")
@RestController
@RequestMapping("/api/bean")
public class BeanApi implements BaseResult {

    @ApiOperation("动态注册bean")
    @PostMapping("/register")
    public ResponseWrapper<?> registerBean(@RequestBody InstanceBeanRequest request) {

        // 将applicationContext转换为ConfigurableApplicationContext
        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) SpringContextHolder.getApplicationContext();

        // 获取bean工厂并转换为DefaultListableBeanFactory
        DefaultListableBeanFactory listableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();

        // 通过BeanDefinitionBuilder创建bean定义
        SupervisordProperties properties = request.getProps();

        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(SupervisordProperties.class, () -> properties);
        beanDefinitionBuilder.setInitMethodName("init");
        beanDefinitionBuilder.setDestroyMethodName("destroy");

        // 设置属性
        // beanDefinitionBuilder.addPropertyReference("", "");

        // 注册bean
        String beanName = String.join("", request.getKeyName(), StringUtils.capitalize(SupervisordProperties.SUPERVISORD_PREFIX));
        listableBeanFactory.registerBeanDefinition(beanName, beanDefinitionBuilder.getRawBeanDefinition());

        String[] beanDefinitionNames = SpringContextHolder.getBeanDefinitionNames();

        return ok(beanDefinitionNames);
    }

    @ApiOperation("动态移除bean")
    @DeleteMapping("/remove")
    public ResponseWrapper<?> removeBean(String beanName) {

        // 将applicationContext转换为ConfigurableApplicationContext
        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) SpringContextHolder.getApplicationContext();

        // 获取bean工厂并转换为DefaultListableBeanFactory
        DefaultListableBeanFactory listableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();

        // 删除bean.
        listableBeanFactory.removeBeanDefinition(beanName);

        String[] beanDefinitionNames = SpringContextHolder.getBeanDefinitionNames();

        return ok(beanDefinitionNames);
    }
}
